import Vue from 'vue'
import store from '@/store'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, target) {
  return roles.some(role => target.indexOf(role) > -1)
}

Vue.directive('permission', {
  inserted: function(el, binding) {
    if (!hasPermission(binding.value, store.state.user.roles)) {
      el.parentNode.removeChild(el)
    }
  }
})
